/*
 * Copyright 2021-2030 itbox.com All right reserved. This software is the confidential and proprietary information
 * of itbox.com ("Confidential Information"). You shall not disclose such Confidential Information and shall use it
 * only in accordance with the terms of the license agreement you entered into with itbox.com.
 */
package cn.itbox.example.test.controller;

import cn.itbox.example.service.RoleService;
import cn.itbox.example.model.request.CreateRoleRequest;
import cn.itbox.example.model.request.UpdateRoleRequest;
import cn.itbox.example.model.response.RoleResponse;
import cn.itbox.uap.util.http.OkHttpExtUtil;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @BelongProject springboot-study
 * @BelongPackage cn.itbox.example.controller
 * @Description TODO
 * @ClassName UserController
 * @Author xingc
 * @Date 2023/11/26 17:09
 * @Version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/role")
public class RoleController {
    @DubboReference(protocol = "dubbo")
    private RoleService roleService;

    @Resource
    private OkHttpExtUtil okHttpExtUtil;


    @GetMapping
    public String list() {
//        List<RoleResponse> roleDOS = roleService.list();
//        return roleDOS;


        String url = "https://sandbox.99bill.com:7445/umgw/common/distribute.html";
        String body = "{\"head\":{\"memberCode\":\"10012932671\",\"messageType\":\"M0001\",\"version\":\"1.0.0\",\"externalRefNumber\":\"20240221101149_0b19e\"},\"requestBody\":{\"signedData\":\"a3caa15e-26a5-4a90-829b-6e4596f79f07\",\"envelopedData\":\"92280ee7-460e-4e71-a295-b659092f0e57\"}}";
        Map<String, String> headers = new HashMap<>();
        headers.put("okhttp-test", "1");
        String res = okHttpExtUtil.post(url, body, headers, 1000L);

        System.out.printf(res);

        return res;
    }

    @PostMapping()
    public RoleResponse create(@RequestBody @Valid CreateRoleRequest cmd) {
        Long id = roleService.create(cmd);
        RoleResponse rolePageDTO = roleService.byId(id);
        return rolePageDTO;
    }

    @PutMapping
    public RoleResponse update(@RequestBody @Valid UpdateRoleRequest cmd) {
        roleService.update(cmd);
        RoleResponse rolePageDTO = roleService.byId(cmd.getId());
        return rolePageDTO;
    }

    @DeleteMapping("/{id}")
    public void delete(@PathVariable Long id) {
        roleService.delete(id);
    }
}
